package com.xyz.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyz.entity.Appeal;
import com.xyz.entity.VehicleOwnerInfo;
import com.xyz.entity.ViolationRecord;
import com.xyz.entity.request.AppealRecordRequest;
import com.xyz.entity.request.ChangeAppealRequest;

import com.xyz.entity.vo.ListAppealVO;
import com.xyz.mapper.AppealMapper;
import com.xyz.service.IAppealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyz.service.IVehicleOwnerInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 申诉表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-04-27
 */
@Service
public class AppealServiceImpl extends ServiceImpl<AppealMapper, Appeal> implements IAppealService {
    private final IVehicleOwnerInfoService vehicleOwnerInfoService;
    private final ViolationRecordServiceImpl violationRecordService;

    public AppealServiceImpl(IVehicleOwnerInfoService vehicleOwnerInfoService, ViolationRecordServiceImpl violationRecordService) {
        this.vehicleOwnerInfoService = vehicleOwnerInfoService;
        this.violationRecordService = violationRecordService;
    }

    @Override
    public void postAppeal(AppealRecordRequest request) {
        Appeal appeal = new Appeal();
        BeanUtils.copyProperties(request, appeal);
        this.save(appeal);
    }

    @Override
    public Page<ListAppealVO> listAppeal(Integer page, Integer limit) {
        Page<Appeal> page1 = this.lambdaQuery().select(Appeal::getId, Appeal::getReason, Appeal::getStatus, Appeal::getCreatedAt, Appeal::getReasonForFailure, Appeal::getRecordId).page(new Page<>(page, limit));
        List<ListAppealVO> collect = page1.getRecords().stream().map(o -> {
            ListAppealVO item = new ListAppealVO();
            BeanUtils.copyProperties(o, item);
            item.setStatus(this.getStatus(o.getStatus()));
            return item;
        }).collect(Collectors.toList());
        Page<ListAppealVO> listAppealVOPage = new Page<>();
        BeanUtils.copyProperties(page1, listAppealVOPage);
        listAppealVOPage.setRecords(collect);
        return listAppealVOPage;
    }

    @Override
    public Page<ListAppealVO> listAppeal(String idNumber, Integer page, Integer limit) {
        List<VehicleOwnerInfo> list = this.vehicleOwnerInfoService.lambdaQuery().select(VehicleOwnerInfo::getPlateNumber).eq(VehicleOwnerInfo::getIdNumber, idNumber).list();
        List<String> plateNumbers = list.stream().map(VehicleOwnerInfo::getPlateNumber).collect(Collectors.toList());
        List<ViolationRecord> list1 = this.violationRecordService.lambdaQuery().select(ViolationRecord::getId).in(ViolationRecord::getPlateNumber, plateNumbers).list();
        List<Long> violationRecordIds = list1.stream().map(ViolationRecord::getId).collect(Collectors.toList());
        Page<Appeal> page1 = this.lambdaQuery().select(Appeal::getId, Appeal::getReason, Appeal::getStatus, Appeal::getCreatedAt, Appeal::getReasonForFailure, Appeal::getRecordId).in(Appeal::getRecordId, violationRecordIds).page(new Page<>(page, limit));
        List<ListAppealVO> collect = page1.getRecords().stream().map(o -> {
            ListAppealVO item = new ListAppealVO();
            BeanUtils.copyProperties(o, item);
            item.setStatus(this.getStatus(o.getStatus()));
            return item;
        }).collect(Collectors.toList());
        Page<ListAppealVO> listAppealVOPage = new Page<>();
        BeanUtils.copyProperties(page1, listAppealVOPage);
        listAppealVOPage.setRecords(collect);
        return listAppealVOPage;
    }

    @Override
    public void changeAppeal(ChangeAppealRequest request) {
        LambdaUpdateWrapper<Appeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Appeal::getStatus, request.getStatus()).eq(Appeal::getId, request.getId());
        if (StringUtils.isNotEmpty(request.getReasonForFailure()) && StringUtils.isNotEmpty(request.getReasonForFailure())) {
            wrapper.set(Appeal::getReasonForFailure, request.getReasonForFailure());
        }
        this.update(wrapper);
        if (request.getStatus() == 1) {
            this.violationRecordService.lambdaUpdate().set(ViolationRecord::getIsProcessed, 1).eq(ViolationRecord::getId, request.getRecordId()).update();
        }
    }

    private String getStatus(Integer status) {
        switch (status) {
            case 0:
                return "正在申诉";
            case 1:
                return "申诉成功";
            case 2:
                return "申诉失败";
            default:
                return "错误";
        }
    }
}
